<template lang="pug">
  .CompleteCommercialWagesModal
    Modal(
      v-model="isShowCompleteCommercialWagesModal"
      :title="completeCommercialWagesModalTitle"
      class-name="vertical-center-modal"
      width="480"
    )
      .clearfix
        Form.ivu-row.has-rules-item2(ref="formData" :model="formData" :rules="rules" :label-width="130")
          FormItem.form-item.ivu-col(v-for="(item, index) of formItem" :label="item.label" :key="index" :class="item.style" :prop="item.prop")
            template(v-if="item.type === 'input'")
              Input(v-model="formData[item.model]" :placeholder="item.placeholder" clearable)
            template(v-if="item.type === 'select'")
              Select(v-model="formData[item.model]" :placeholder="item.placeholder" clearable)
                Option(v-for="(c_item, c_index) of arrListDown[item.options]" :key="c_index" :value="c_item.value") {{c_item.text}}
            template(v-if="item.type === 'textarea'")
              Input(v-model="formData[item.model]" :placeholder="item.placeholder" type="textarea" :rows="3")
            template(v-if="item.type === 'datePicker'")
              DatePicker.w-100(v-model="formData[item.model]" :placeholder="item.placeholder" type="date" format="yyyy-MM-DD")
            template(v-if="item.type === 'upload'")
              Row
                Col(span="24")
                  Upload(
                    ref="upload"
                    :max-size="10 * 1024"
                    :on-exceeded-size="handleMaxSizeTen"
                    :before-upload="handleBeforeUpload"
                    :on-success="uploadSuccess"
                    accept=".xls, .xlsx"
                    :action="baseUrl + 'api/Treatment/TreatmentEnjoyLinkAnnexUpload'"
                    :show-upload-list="false"
                    :data="formData"
                  )
                    Input(v-model="formData[item.model].name" v-show="false")
                    Button.mr-20(type="info") 附件上传
                    Tag.mr-10(v-if="files.length > 0" v-for="file of files") {{file.name}}
                Col(span="24")
                  .text-gray-9 仅支持上传10M以内的xlsx文件
        
      .clearfix(slot="footer")
        Button(type="text" @click="cancel") 取消
        Button(type="primary" @click="handleSave" v-if="actionType !== 'view'" :loading="!isCanSubmit") 确定
</template>

<script>
import { Common } from '@/utils/common.js'
import dataApi from '@/api/data.js'
import { TreatmentApi } from '@/api/Treatment.api.js'
import { createNodeThead, editNodeThead } from '../tableHead'
import _cloneDeep from 'clone-deep'

export default {
  name: 'CompleteCommercialWagesModal',

  watch: {
  },

  computed: {
    setTeadHead () {
      if (this.actionType === 'view') {
        return this.editNodeThead.slice(0, this.editNodeThead.length - 1)
      } else if (this.actionType === 'edit') {
        return this.editNodeThead
      } else {
        return this.createNodeThead
      }
    }
  },

  data () {
    return {
      baseUrl: baseUrl,
      id: '',
      rowVersion: '',
      isShowCompleteCommercialWagesModal: false,
      completeCommercialWagesModalTitle: '商保待遇办理完成',
      actionType: 'view',
      wagesId: '',
      formData: {
        completionTime: '',
        processingDescription: '',
        // levelId: '',
        amount: '',
        fileAnnex: []
      },
      formItem: [
        { type: 'datePicker', label: '完成办理时间：', placeholder: '请选择完成办理时间', prop: 'completionTime', model: 'completionTime', style: 'ivu-col-span-24' },
        { type: 'input', label: '办理说明：', placeholder: '请输入办理说明', prop: 'processingDescription', model: 'processingDescription', style: 'ivu-col-span-24' },
        // { type: 'input', label: '鉴定等级：', placeholder: '请输入鉴定等级', prop: 'levelId', model: 'levelId', style: 'ivu-col-span-24' },
        { type: 'input', label: '金额：', placeholder: '请输入金额', prop: 'amount', model: 'amount', style: 'ivu-col-span-24' },
        { type: 'upload', label: '附件：', placeholder: '请上传附件', prop: 'fileAnnex', model: 'fileAnnex', style: 'ivu-col-span-24 is-required' },
      ],
      rules: {
        completionTime: [
          { required: true, message: '请选择完成办理时间', type: 'date', trigger: 'change' }
        ],
        processingDescription: [
          { required: true, message: '请输入办理说明', trigger: 'change' }
        ],
        levelId: [
          { required: true, message: '请输入鉴定等级', trigger: 'change' }
        ],
        amount: [
          { required: true, message: '请输入金额', trigger: 'change' }
        ],
        fileAnnex: [
          { required: true, message: '请上传附件', trigger: 'change' }
        ]
      },
      arrListDown: {
        tempOps: []
      },
      areaTree: [], // 中国所有城市树
      createNodeThead: _cloneDeep(createNodeThead),
      editNodeThead: _cloneDeep(editNodeThead),
      nodeTable: [],
      errorData: {
        errorId: '',
        isError: false,
        downLoadUrl: null
      },
      files: [],
      fileAnnexArr: [],
      isCanSubmit: true
    }
  },

  mounted () {
  },

  methods: {
    // 监听所有调用的方法
    listenCall (methodsWords, param) {
      this[methodsWords](param)
    },

    cancel () {
      this.isShowCompleteCommercialWagesModal = false
    },

    show (actionType, id, rowVersion) {
      this.actionType = actionType
      this.id = id
      this.rowVersion = rowVersion
      console.log('actionType: ', actionType, 'id: ', id)
      this.isShowCompleteCommercialWagesModal = true
      this.$refs.formData.resetFields()
      this.nodeTable = []
    },

    // 日期选择
    handleChangeDatePicker (model, val) {
      console.log('model: ', model)
      console.log('val: ', val)
      this.formData[model] = val
    },

    // 根据ID获取待遇参数
    async getModel (id) {
      let res = await SysBasedataApi.getModel(id)
      console.log('根据ID获取待遇参数: ', res)
    },

    // // 新增到表格
    // handleCreateToTable () {
    //   console.log('this.formData', this.formData)
    //   var tempObj = {...this.formData}
    //   this.nodeTable.push(tempObj)
    //   this.$refs.formData.resetFields()
    // },

    // // 从表格删除
    // handleDelRow (index) {
    //   this.nodeTable.splice(index, 1)
    // },

    handleMaxSizeTen () {
      this.$Message.error({
        content: '您上传的文件太大，上传文件大小不能超过50M',
        duration: 50,
        closable: true
      })
    },

    // 批量导入-上传前文件
    handleBeforeUpload (file) {
      this.errorData.isError = false
      this.files.push(file)
      console.log('files: ', this.files)
      // return false
    },

    // 上传成功
    uploadSuccess (res, row) {
      if (res.Success) {
        var idArr = []
        this.fileAnnexArr.push({...res.Data, ...{TreatmentEnjoyLinkID: this.id, Annex: res.Data.Id}})
        this.fileAnnexArr.map(item => {
          delete item.Id
          idArr.push(item.Annex)
        })
        this.formData.fileAnnex = idArr.join(',')
      } else {
        this.$Message.success(res.Message)
      }
    },

    // 普通附件下载
    accessoryDownload (url) {
      console.log('下载url: ', url)
      window.open(url, '_blank')
    },

    // 保存
    handleSave () {
      this.$refs.formData.validate((valid) => {
        if (valid) {
          this.isCanSubmit = false
          var params = {
            Id: this.id,
            RowVersion: this.rowVersion,
            fileAnnex: this.fileAnnexArr
          }
          // delete this.formData.fileAnnex
          console.log('params: ', {...this.formData, ...params})
          TreatmentApi.completionTreatmentEnjoy({...this.formData, ...params}).then(res => {
            console.log('保存: ', res)
            if (res.data.success) {
              this.$Message.success(res.data.message)
              this.$emit('query')
              this.isShowCompleteCommercialWagesModal = false
            } else {
              this.isCanSubmit = true
              this.$Message.error({
                content: res.data.message,
                duration: 10,
                closable: true
              })
            }
          })
        } else {
          this.isCanSubmit = true
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>

</style>
